package com.Sun.DB;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Statement;

public class DBUtil {
	private static String url= "jdbc:mysql://localhost:3306/paper_lib?"+"useUnicode=true&characterEncoding=UTF8";
	private static String username="root";
	private static String pwd="985892";
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 *获得一个数据库连接
	 * @conn
	 * @throws SQLException
	 */
	public static Connection getConnection() throws SQLException
	{
		Connection conn=DriverManager.getConnection(url, username, pwd);
		return conn;
	}
	/**
	 * 关闭连接
	 * @param conn
	 * @param stmt
	 * @param rs
	 * @throws SQLException
	 */
	public static void CloseConn(Connection conn,Statement stmt,ResultSet rs) throws SQLException
	{
		if(rs!=null)
		{
			rs.close();
			rs=null;
		}
		if(stmt!=null)
		{
			stmt.close();
			stmt=null;
		}
		if(conn!=null)
		{
			conn.close();
			conn=null;
		}	
	}
	public static void CloseConn(Connection conn,Statement stmt) throws SQLException
	{
		if(stmt!=null)
		{
			stmt.close();
			stmt=null;
		}
		if(conn!=null)
		{
			conn.close();
			conn=null;
		}	
	}
}
